package com.orange.core.pojo.entity.permission;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.orange.core.pojo.entity.IIntIdModel;
import com.orange.core.pojo.entity.IOperatorModel;
import com.orange.core.util.Constants;
import lombok.Data;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Accessors(chain = true)
public class Role implements IIntIdModel<Role>, IOperatorModel<Role> {

    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 角色名
     */
    private String name;

    /**
     * 角色编码
     */
    private String code;

    /**
     * 状态：0禁用，1启用
     *
     * @see com.orange.core.pojo.enums.EStatus
     */
    private Integer status;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private LocalDateTime createTime;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @TableField(fill = FieldFill.INSERT)
    private Integer createBy;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private LocalDateTime updateTime;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Integer updateBy;

    @TableField(exist = false)
    private List<Integer> permissionIds;

    public String getCode() {
        if (StrUtil.isNotBlank(this.code) && !this.code.startsWith(Constants.ROLE_PREFIX)) {
            this.code = Constants.ROLE_PREFIX + this.code;
        }
        return this.code;
    }
}
